<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="支出类型" prop="expenditureType">
        <el-select v-model="queryParams.expenditureType" placeholder="请选择支出类型" clearable size="small">
          <el-option v-for="iteam in expenditureDicts" :label="iteam.dictLabel" :value="iteam.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="支出金额" prop="expenditureAmount">
        <el-input v-model="queryParams.expenditureAmount" placeholder="请输入支出金额" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否发票" prop="isBill">
        <el-select v-model="queryParams.isBill" placeholder="请选择是否发票" clearable size="small"
          @keyup.enter.native="handleQuery">
          <el-option v-for="iteam in isDicts" :label="iteam.dictLabel" :value="iteam.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:expenditure:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:expenditure:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:expenditure:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
          @click="handleExport" v-hasPermi="['system:expenditure:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="expenditureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="支出类型" align="center" prop="expenditureType">
        <template slot-scope="scope">
          <span v-if="scope.row.expenditureType==0">工资</span>
          <span v-else-if="scope.row.expenditureType==1">办公用品</span>
          <span v-else-if="scope.row.expenditureType==2">交通 (出差)</span>
          <span v-else-if="scope.row.expenditureType==4">房租</span>
          <span v-else>其他</span>
        </template>
      </el-table-column>
      <el-table-column label="支出时间" align="center" prop="expenditureTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expenditureTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支出金额" align="center" prop="expenditureAmount" />
      <el-table-column label="支出备注" align="center" prop="expenditureNotes" />
      <el-table-column label="是否发票" align="center" prop="isBill">
        <template slot-scope="scope">
          <span v-if="scope.row.isBill=0">否</span>
          <span v-else>是</span>
        </template>
      </el-table-column>
      <el-table-column label="报销时间" align="center" prop="reimbursementTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reimbursementTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报销人" align="center" prop="reimbursementer">
        <template slot-scope="scope">
          <span v-if="scope.row.reimbursementer">{{ scope.row.reimbursementer.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditer">
        <template slot-scope="scope">
          <span v-if="scope.row.auditer">{{ scope.row.auditer.nickName }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人时间" align="center" prop="auditerTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditerTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-folder-checked" @click="handleDetails(scope.row)"
            v-hasPermi="['system:expenditure:audite']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-folder-checked" @click="handleAudite(scope.row)"
            v-hasPermi="['system:expenditure:audite']">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-folder-checked" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:expenditure:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:expenditure:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改支出对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="支出类型" prop="expenditureType">
          <el-select v-model="form.expenditureType" placeholder="请选择支出类型 0：工资，1：办公用品，2：交通">
            <el-option v-for="iteam in expenditureDicts" :label="iteam.dictLabel" :value="iteam.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="支出时间" prop="expenditureTime">
          <el-date-picker clearable size="small" v-model="form.expenditureTime" type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择支出时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支出金额" prop="expenditureAmount">
          <el-input v-model="form.expenditureAmount" placeholder="请输入支出金额" />
        </el-form-item>
        <el-form-item label="支出备注" prop="expenditureNotes">
          <el-input v-model="form.expenditureNotes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否发票" prop="isBill">
          <el-select v-model="form.isBill" placeholder="请选择是否发票">
            <el-option v-for="iteam in isDicts" :label="iteam.dictLabel" :value="iteam.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="支出凭证" prop="expenditureUrls">
          <ImageUpload v-model="form.expenditureUrls"></ImageUpload>
        </el-form-item>
        <el-form-item label="报销人" prop="reimbursementerId">
          <el-select v-model="form.reimbursementerId" placeholder="请选择报销人">
            <el-option v-for="iteam in userList" :label="iteam.nickName" :value="iteam.userId" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核 -->
    <el-dialog title="审核" :visible.sync="auditeOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="支出类型">
          <el-select v-model="form.expenditureType" placeholder="请选择支出类型 0：工资，1：办公用品，2：交通" disabled>
            <el-option v-for="iteam in expenditureDicts" :label="iteam.dictLabel" :value="iteam.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="支出时间">
          <el-date-picker clearable size="small" v-model="form.expenditureTime" type="datetime" placeholder="选择支出时间"
            value-format="yyyy-MM-dd HH:mm:ss" disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支出金额">
          <el-input v-model="form.expenditureAmount" placeholder="请输入支出金额" disabled />
        </el-form-item>
        <el-form-item label="支出备注">
          <el-input v-model="form.expenditureNotes" type="textarea" placeholder="请输入内容" disabled />
        </el-form-item>
        <el-form-item label="是否发票">
          <el-select v-model="form.isBill" placeholder="请选择是否发票" disabled>
            <el-option v-for="iteam in isDicts" :label="iteam.dictLabel" :value="iteam.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="支出凭证">
          <ImageUpload v-model="form.expenditureUrls" disabled></ImageUpload>
        </el-form-item>
        <el-form-item label="报销时间">
          <el-date-picker clearable size="small" v-model="form.reimbursementTime" type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择报销时间" disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报销人">
          <el-select v-model="form.reimbursementerId" placeholder="请选择报销人" disabled>
            <el-option v-for="iteam in userList" :label="iteam.nickName" :value="iteam.userId" />
          </el-select>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditerNotes">
          <el-input v-model="form.auditerNotes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="submitAuditeForm(false)">不通过</el-button>
        <el-button type="primary" @click="submitAuditeForm(true)">通过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 审核 -->
    <el-dialog title="详情" :visible.sync="detailsOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="支出类型">
          <el-select v-model="form.expenditureType" placeholder="请选择支出类型 0：工资，1：办公用品，2：交通" disabled>
            <el-option v-for="iteam in expenditureDicts" :label="iteam.dictLabel" :value="iteam.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="支出时间">
          <el-date-picker clearable size="small" v-model="form.expenditureTime" type="datetime" placeholder="选择支出时间"
            value-format="yyyy-MM-dd HH:mm:ss" disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支出金额">
          <el-input v-model="form.expenditureAmount" placeholder="请输入支出金额" disabled />
        </el-form-item>
        <el-form-item label="支出备注">
          <el-input v-model="form.expenditureNotes" type="textarea" placeholder="请输入内容" disabled />
        </el-form-item>
        <el-form-item label="是否发票">
          <el-select v-model="form.isBill" placeholder="请选择是否发票" disabled>
            <el-option v-for="iteam in isDicts" :label="iteam.dictLabel" :value="iteam.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="支出凭证">
          <ImageUpload v-model="form.expenditureUrls" disabled></ImageUpload>
        </el-form-item>
        <el-form-item label="报销时间">
          <el-date-picker clearable size="small" v-model="form.reimbursementTime" type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择报销时间" disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报销人">
          <el-select v-model="form.reimbursementerId" placeholder="请选择报销人" disabled>
            <el-option v-for="iteam in userList" :label="iteam.nickName" :value="iteam.userId" />
          </el-select>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditerNotes">
          <el-input v-model="form.auditerNotes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {
    listExpenditure,
    getExpenditure,
    delExpenditure,
    addExpenditure,
    updateExpenditure,
    exportExpenditure,
    auditeExpenditure
  } from "@/api/system/expenditure";
  import {
    listUser
  } from "@/api/system/user";

  // import ImageUpload form '@/compoents/ImageUpload/index'


  export default {
    name: "Expenditure",
    data() {
      return {
        uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 支出表格数据
        expenditureList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //审核dialog开关
        auditeOpen: false,
        //详情开关
        detailsOpen:false,
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          expenditureType: null,
          expenditureTime: null,
          expenditureAmount: null,
          expenditureNotes: null,
          isBill: null,
          expenditureUrls: null,
          reimbursementTime: null,
          reimbursementUrls: null,
          reimbursementerId: null,
          auditerId: null,
          auditerTime: null,
          auditerNotes: null,
          isDel: null,
          createdBy: null,
          createdTime: null,
          updatedBy: null,
          updatedTime: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          expenditureType: [{
            required: true,
            message: '请选择',
            trigger: 'change'
          }],
          expenditureTime: [{
            required: true,
            message: '请选择',
            trigger: 'change'
          }],
          expenditureAmount: [{
            required: true,
            message: '请输入',
            trigger: 'blur'
          }, ],
          expenditureNotes: [{
              required: true,
              message: '请输入内容',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 100,
              message: '请正确输入',
              trigger: 'blur'
            }
          ],
          isBill: [{
            required: true,
            message: '请选择',
            trigger: 'change'
          }],
          expenditureUrls: [{
            required: true,
            message: '请选择',
            trigger: 'change'
          }],
          reimbursementTime: [{
            required: true,
            message: '请选择',
            trigger: 'change'
          }],
          reimbursementUrls: [{
            required: true,
            message: '请选择',
            trigger: 'change'
          }],
          reimbursementerId: [{
            required: true,
            message: '请选择',
            trigger: 'change'
          }],
          auditerNotes: [{
              required: true,
              message: '请输入内容',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 100,
              message: '请添加备注',
              trigger: 'blur'
            }
          ],

        },
        expenditureDicts: [],
        isDicts: [],
        userList: [],


      };
    },
    created() {
      this.getList();
      this.getUserList();

      this.getDicts("expenditure_type").then(response => {
        let expenditureDicts = response.data;
        for (var i = 0; i < expenditureDicts.length; i++) {
          let expenditureDict = expenditureDicts[i]
          // expenditureDict.dictValue = parseInt(expenditureDict.dictValue)
        }
        this.expenditureDicts = expenditureDicts
      });

      this.getDicts("sys_yes_no").then(response => {
        let isDicts = response.data;
        for (var i = 0; i < isDicts.length; i++) {
          let isDict = isDicts[i]
          isDict.dictValue = parseInt(isDict.dictValue)
        }
        this.isDicts = isDicts
      });
    },


    methods: {
      /** 查询用户列表 */
      getUserList() {
        this.loading = true;
        listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询支出列表 */
      getList() {
        this.loading = true;
        listExpenditure(this.queryParams).then(response => {
          this.expenditureList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          expenditureType: null,
          expenditureTime: null,
          expenditureAmount: null,
          expenditureNotes: null,
          isBill: null,
          expenditureUrls: null,
          reimbursementTime: null,
          reimbursementUrls: null,
          reimbursementerId: null,
          auditerId: null,
          auditerTime: null,
          isDel: null,
          createdBy: null,
          createdTime: null,
          updatedBy: null,
          updatedTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加支出";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getExpenditure(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改支出";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateExpenditure(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addExpenditure(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      submitAuditeForm(isPass) {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.isPass=isPass;
            auditeExpenditure(this.form).then(response => {
              this.msgSuccess("审核成功");
              this.auditeOpen = false;
              this.getList();
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除支出编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delExpenditure(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有支出数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportExpenditure(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      },
      /** 审核提交操作 */
      handleAudite(row) {
        this.reset();
        const id = row.id || this.ids
        getExpenditure(id).then(response => {
          this.form = response.data;
          this.auditeOpen = true;
        });
      },
      handleDetails(){
        this.reset();
        const id = row.id || this.ids
        getExpenditure(id).then(response => {
          this.form = response.data;
          this.detailsOpen = true;
        });
      }
    },
  }
</script>
